package com.dragons.dragonsvf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragons.dragonsvf.dao.MemeryInfoMapper;
import com.dragons.dragonsvf.entities.*;
import com.dragons.dragonsvf.utils.netUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dragons.G
 * @date 2025-03-26 21:27:59
 * @description 回忆录
 */
@RestController
public class MemeryInfoCtrl {

    private static final Logger logger = LoggerFactory.getLogger(MemeryInfoCtrl.class);

    @Autowired
    private MemeryInfoMapper memeryInfoMapper;

    /** 获取回忆录数据 */
    @RequestMapping("/getMemeryList")
    public ResponseDTO getMemerys(@RequestBody MemeryInfoDTO memeryInfoDTO) {
        List<MemeryInfo> m = memeryInfoMapper.selectList(null);
        List<MemeryInfoDTO> result = new ArrayList<>();
        m.stream().forEach(item -> {
            MemeryInfoDTO memery = new MemeryInfoDTO();
            memery.setId(item.getId());
            memery.setImgTitle(item.getImg_title());
            memery.setIngDescription(item.getIng_description());
            memery.setImgTags(item.getImg_tags());
            memery.setImgLetterUnchk(item.getImg_letter_unchk());
            memery.setImgLetterChk(item.getImg_letter_chk());
            memery.setImgLike(item.getImg_like());
            memery.setImgComment(item.getImg_comment());
            memery.setImgScan(item.getImg_scan());
            memery.setImgMinioRrl(item.getImg_minio_url());
            memery.setImgFreezeOrnnt(item.getImg_freeze_ornnt());
            memery.setImgShowedRole(item.getImg_showed_role());
            memery.setImgRecordTm(item.getImg_record_tm());
            memery.setImgUpdateTm(item.getImg_update_tm());
            String avatarBase64 = netUtils.convertToBase64(item.getImg_minio_url());
            memery.setAvatarBase64(avatarBase64);
            memery.setPageSize(1);
            memery.setCurrentPage(1);
            result.add(memery);
        });
        ResponseDTO response = new ResponseDTO();
        response.setCode(200);
        response.setData(result);
        response.setMsg("查询回忆成功！");
        return response;
    }


    /**
     * 根据 TAB 的切换查询数据
     * @param mid
     * @return
     */
    @RequestMapping("/getMemeryListByTab")
    public ResponseDTO getMemerysByTab(@RequestBody MemeryInfoDTO mid) {

        QueryWrapper<MemeryInfo> q = new QueryWrapper<>();
        q.eq("img_showed_role", mid.getImgShowedRole());
        Long aLong = memeryInfoMapper.selectCount(q);

        Page<MemeryInfo> page = new Page<>(mid.getCurrentPage(), mid.getPageSize());
        Page<MemeryInfo> mip = memeryInfoMapper.selectPage(page, q);
        List<MemeryInfo> m = mip.getRecords();

        long pages = mip.getPages();
        long size = mip.getSize();

        List<MemeryInfoDTO> result = new ArrayList<>();
        m.stream().forEach(item -> {
            MemeryInfoDTO memery = new MemeryInfoDTO();
            memery.setId(item.getId());
            memery.setImgTitle(item.getImg_title());
            memery.setIngDescription(item.getIng_description());
            memery.setImgTags(item.getImg_tags());
            memery.setImgLetterUnchk(item.getImg_letter_unchk());
            memery.setImgLetterChk(item.getImg_letter_chk());
            memery.setImgLike(item.getImg_like());
            memery.setImgComment(item.getImg_comment());
            memery.setImgScan(item.getImg_scan());
            memery.setImgMinioRrl(item.getImg_minio_url());
            memery.setImgFreezeOrnnt(item.getImg_freeze_ornnt());
            memery.setImgShowedRole(item.getImg_showed_role());
            memery.setImgRecordTm(item.getImg_record_tm());
            memery.setImgUpdateTm(item.getImg_update_tm());
            //String avatarBase64 = netUtils.convertToBase64(item.getImg_minio_url());
            //memery.setAvatarBase64(avatarBase64);
            memery.setPageSize(1);
            memery.setCurrentPage(1);
            memery.setImgIsChk(item.getImg_is_chk());
            result.add(memery);
        });
        ResponseDTO response = new ResponseDTO();
        response.setCode(200);
        response.setData(result);
        response.setMsg("查询回忆成功！");
        return response;
    }


    @RequestMapping("/updateMemeryData")
    public ResponseDTO updateMemeryData(@RequestBody MemeryInfoDTO mid) {
        MemeryInfo upd = new MemeryInfo();
        upd.setId(mid.getId());
        // 交换两个值
//        upd.setImg_letter_chk(mid.getImgLetterChk());
//        upd.setImg_letter_unchk(mid.getImgLetterUnchk());
        upd.setImg_is_chk(mid.getImgIsChk());
        upd.setImg_like(mid.getImgLike());
        upd.setImg_scan(mid.getImgScan());
        int i = memeryInfoMapper.updateById(upd);
        ResponseDTO response = new ResponseDTO();
        if (i > 0) {
            response.setCode(200);
            response.setData(i);
            response.setMsg("文案更新成功！");
        } else {
            response.setCode(400);
            response.setData(i);
            response.setMsg("文案更新失败！");
        }
        return response;
    }


}
